// pages/stulogin/stulogin.js
const db = wx.cloud.database()

Page({

  /**
   * 页面的初始数据
   */
  data: {
    info:[]
  },

  btnSub:function(btnRes){
    var that=this
    var name=btnRes.detail.value.name;
    var ID=btnRes.detail.value.input_id;
    var school=btnRes.detail.value.school_id;
    var openid=that.data.info[0];
    var nickName=that.data.info[1];
    var avatarUrl=that.data.info[2];
    wx.cloud.callFunction({
      name:"find",
      data:{
        name_collection:"input_table",
        condition:{
          input_data:name,
          input_id:ID,
          school_id:school,
          permissions_level:"1"
        }
      },
      success(login){
        console.log(login);
        if(login.result.data.length!=0){
          if(login.result.data[0].open_id!=""){
            wx.showModal({title:'该学生已被注册！'})
            return;
          }
          wx.cloud.callFunction({
            name:"update",
            data:{
              name_collection:"input_table",
              condition:{
                input_data:name,
                input_id:ID,
                school_id:school
              },
              data:{
                open_id:openid
              }//数据
            },
          })
          let college_id=login.result.data[0].college_id;
          let input_class=login.result.data[0].input_class;
          let input_data=login.result.data[0].input_data;
          let input_id=login.result.data[0].input_id;
          let permissions_level = login.result.data[0].permissions_level;
          let school_id=login.result.data[0].school_id;
          let school_name=login.result.data[0].school_name;
          let avatarUrl=that.data.info[2];
          let Info={
            "college_id":college_id,
            "input_class":input_class,
            "input_data":input_data,
            "input_id":input_id,
            "permissions_level":permissions_level,
            "school_id":school_id,
            "school_name":school_name,
            "avatarUrl":avatarUrl,
          };
          wx.hideLoading({
            success: (res) => {
              wx.showToast({
                title: '注册成功',
                icon:'success',
                duration:200
              })
            },
          })
          wx.reLaunch({
          url: '/pages/stu-information/stu-information?info='+JSON.stringify({Info}) //注册成功，跳转到学生个人页面
          })
        }
        else{
          wx.showModal({title:'信息错误，请重新输入！'})
        }
      },

    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that=this
    let dataArr = options.Info.split(',');
    that.setData({
      info:dataArr
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})